﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>消息管理</title>
    <script src="../Scripts/jquery-1.8.3.js" type="text/javascript"></script>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="../scripts/boot.js" type="text/javascript"></script>
    <link href="../css/common.css" rel="stylesheet" type="text/css" />
    <script src="../scripts/stringExtend.js" type="text/javascript"></script>
</head>
<body>
    <div style="width: 100%;">
        <div id="toolbar">
            <div class="marginT-B5">
                <table style="width: auto;">
                    <tr>
                        <td>
                            消息类别:
                        </td>
                        <td>
                            <select id="MessageTypeSearch">
                                <option value="">全部</option>
                                <option value="1">系统消息</option>
                                <option value="2">审核消息</option>
                            </select>
                        </td>
                        <td>
                            消息等级：
                        </td>
                        <td>
                            <select id="MessagePrioritySearch">
                                <option value="">全部</option>
                                <option value="1">普通</option>
                                <option value="2">重要</option>
                                <option value="3">紧急</option>
                            </select>
                        </td>
                        <td>
                            <select id="ReadedSearch">
                                <option value="">全部</option>
                                <option value="0">未读</option>
                                <option value="1">已读</option>
                            </select>
                        </td>
                        <td>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-search" onclick="searchs()">查询</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-redo" onclick="resetting()">
                                重置</a>
                        </td>
                        <td>
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-no" onclick="delUserMessageMulti()">批量删除</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <table id="dg" title="消息列表">
        <thead>
            <tr>
            </tr>
        </thead>
    </table>
    <div id="dgdetails">
        <table align="center" width="100%" class="newtab" cellpadding="0" cellspacing="0">
            <tr>
                <td class="right width100">
                    发件人
                </td>
                <td id="detailSender">
                </td>
            </tr>
            <tr>
                <td class="right width100">
                    标题：
                </td>
                <td id="detailTitle">
                </td>
            </tr>
            <tr>
                <td class="right width100">
                    发送时间：
                </td>
                <td id="detailSendTime">
                </td>
            </tr>
            <tr>
                <td class="right width100">
                    消息类别：
                </td>
                <td id="detailTypeName">
                </td>
            </tr>
            <tr>
                <td class="right width100">
                    消息等级：
                </td>
                <td id="detailPriorityName">
                </td>
            </tr>
            <tr>
                <td class="right width100">
                    内容：
                </td>
                <td>
                    <div id="detailContent" class="contentin">
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">
        //查询
        function searchs()
        {
            $('#dg').datagrid(
                'load',
                {
                    MessageType: $("#MessageTypeSearch").val(),
                    MessagePriority: $("#MessagePrioritySearch").val(),
                    ReadedSearch: $("#ReadedSearch").val()
                });
        }
        function resetting()
        {
            $("#MessageTypeSearch").val("");
            $("#MessagePrioritySearch").val("");
            $("#ReadedSearch").val("");
            $('#dg').datagrid('load',
                {
                    MessageType: $("#MessageTypeSearch").val(),
                    MessagePriority: $("#MessagePrioritySearch").val(),
                    ReadedSearch: $("#ReadedSearch").val()
                });
        }
        function delUserMessage()
        {
            var sender = this;
            $.messager.confirm('删除确认', '确定要删除此记录吗？', function (r)
            {
                if (r)
                    delUserMessageHandler(sender);
            });
        }
        //删除用户消息事件
        function delUserMessageHandler(sobj)
        {
            var rowId = $(sobj).attr("sid");
            $(":submit,:button,:reset").attr("disabled", true);
            $.ajax({
                type: "Get",
                dataType: "text",
                url: "List.aspx",
                data:
                {
                    id: rowId,
                    method: "Delete",
                    rid: Math.random()
                }
                ,
                success: function (msg)
                {
                    if (msg.indexOf("已成功") == -1)
                    {
                        $.messager.alert('提示', msg, 'error');
                    }
                    else
                    {
                        //移除html显示
                        //deleteRow
                        $('#dg').datagrid('deleteRow', lastSelectRowIndex);
                        //sobj.parent().parent().remove();
                        $.messager.alert('提示', msg, 'info');
                    }
                    $(":submit,:button,:reset").attr("disabled", false);
                }
            });
        }
        $('#dgdetails').dialog({
            title: '消息详情',
            width: 550,
            height: 390,
            closed: true,
            cache: false,
            modal: true,
            resizable: true,
            collapsible: true,
            maximizable: true
        });
        function details()
        {
            var jobj = $(this);
            var id = jobj.attr("sid");
            $.ajax({
                type: "Get",
                dataType: "text",
                url: "List.aspx",
                data:
                {
                    method: "GetDetail",
                    id: id,
                    rnd: Math.random()
                },
                success: function (data)
                {
                    if (data)
                    {
                        var objjson = jQuery.parseJSON(data);
                        checkLogin(objjson, document);
                        var cobj = objjson[0];
                        $("#detailTitle").text(cobj.Title);
                        $("#detailTypeName").text(cobj.TypeName);
                        $("#detailPriorityName").text(cobj.PriorityName);
                        $("#detailSendTime").text(cobj.SendTime);
                        $("#detailSender").text(cobj.Sender);
                        var re = new RegExp("<(?!a|br|p|img)[^<]*>");
                        $("#detailContent").html(cobj.Content.replace(re, ""));
                        $('#dgdetails').dialog('open').dialog('setTitle', '消息详情');
                        lastSelectrowData.Readed = true;
                        $('#dg').datagrid('updateRow',
                        {
                            index: lastSelectRowIndex,
                            row: lastSelectrowData
                        });
                        $(".details").each(function () { $(this).click(details); });//也许不是最佳方法，因为更新了所有的绑定，但时间很紧，你懂的。
                    }
                    else
                    {
                        $.messager.alert("提示", "加载数据失败", 'error');
                    }
                }
            });
        }
        //批量删除用户消息事件
        function delUserMessageMulti()
        {
            var selectedRow = $("#dg").datagrid('getSelections');
            if (selectedRow.length == 0)
            {
                $.messager.alert('提示', "没有选择任何需要删除的记录", 'error');
                return;
            }
            $.messager.confirm('删除确认', '确定要删除选中的信息吗？', function (r)
            {
                if (r)
                    delUserMessageMultiHandler(selectedRow);
            });
        }
        function delUserMessageMultiHandler(selectedRow)
        {
            var checkedlistId = "";
            var length = selectedRow.length;
            for (var i = 0; i < length; i++)
            {
                checkedlistId += selectedRow[i].MessageId + ",";
            }
            checkedlistId = checkedlistId.substr(0, checkedlistId.length - 1);
            $(":submit,:button,:reset").attr("disabled", true);
            $.ajax({
                type: "Get",
                dataType: "text",
                url: "List.aspx",
                data:
                {
                    idl: checkedlistId,
                    method: "DeleteMulti",
                    rid: Math.random()
                }
                ,
                success: function (msg)
                {
                    if (msg.indexOf("已成功") == -1)
                    {
                        $.messager.alert('提示', msg, 'error');
                    }
                    else
                    {
                        //移除html显示
                        for (i = 0; i < length; i++)
                        {
                            $("#dg").datagrid('deleteRow', $("#dg").datagrid('getRowIndex', selectedRow[i]));
                        }
                        $.messager.alert('提示', msg, 'info');
                    }
                    $(":submit,:button,:reset").attr("disabled", false);
                }
            });
        }
        var lastSelectRowIndex = -1;
        var lastSelectrowData;
        $('#dg').datagrid(
            {
                url: 'List.aspx?method=GetList',
                onLoadSuccess: function ()
                {
                    $(".details").each(function () { $(this).click(details); });
                    $(".del").each(function () { $(this).click(delUserMessage); });
                },
                onLoadError: function (data)
                {
                    if (data.responseText == "logout")
                    {
                        parent.document.location = '/Login.html';
                        //document.location = '/Login.html';
                    }
                    else
                    {
                        $.messager.alert('提示', data.responseText, 'error');
                    }
                },
                onClickRow: function (rowIndex, rowData)
                {
                    lastSelectRowIndex = rowIndex;
                    lastSelectrowData = rowData;
                },
                columns:
                    [
                        [
                            {
                                field: 'controlSelect', align: 'right', rowspan: 2, checkbox: true
                            },
                            { title: '发件人', field: 'Sender', width: 100, rowspan: 2, resizable: true },
                            { title: '消息类别', field: 'TypeName', width: 80, rowspan: 2, resizable: true },
                            { title: '消息等级', field: 'PriorityName', width: 70, rowspan: 2, resizable: true },
                            {
                                title: '标题', field: 'Title', width: 200, rowspan: 2, resizable: true,
                                formatter: function (value)
                                {
                                    return value && value.length > 18 ? value.substr(0, 20) + "..." : value;
                                }
                            },
                            {
                                title: '是否已读', field: 'Readed', width: 60, rowspan: 2, resizable: true,
                                formatter: function (value)
                                {
                                    return value ? "已读" : "未读";
                                },
                                styler: function (value)
                                {
                                    if (!value)
                                    {
                                        return 'color:red;font-weight:bold;';
                                    }
                                }
                            },
                            { title: '发送时间', field: 'SendTime', width: 120, rowspan: 2, resizable: true },
                            { title: '操作', width: 100, align: 'right', colspan: 3, rowspan: 1 }
                        ],
                        [
                            {
                                field: 'text1', title: '详情', width: 70, align: 'right',
                                formatter: function (value, rec)
                                {
                                    return stringFormat('<span class=\"details pointer\" sid=\"{0}\"><span class=\"icon infobig pointer\"></span>详情</span>', rec.MessageId);
                                }
                            },
                            {
                                field: 'text2', title: '删除', width: 70, align: 'right',
                                formatter: function (value, rec)
                                {
                                    return stringFormat('<span class=\"del pointer\" sid=\"{0}\"><span class=\"icon del pointer\"></span>删除</span>', rec.MessageId);
                                }
                            }
                        ]
                    ],
                rowStyler: function ()
                {
                    return 'background-color:#FFFFFF;';
                },
                singleSelect: false,
                pagination: true,
                rownumbers: true,
                showFooter: true,
                checkOnSelect: false,
                striped: true
            });
        $(document).ready(function ()
        {
            $("#deleteMulti").click(delUserMessageMulti);
            $("input[type=radio]").css('vertical-align', 'middle');
        });
    </script>
</body>
</html>
